package cn.edu.zsc.rbac.view;

import cn.edu.zsc.rbac.domain.vo.DeptVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DeptView {
    DeptVo[] cache;
    private static DeptView singleton;
    private Scanner input = new Scanner(System.in);
    private List<DeptVo> deptList = new ArrayList<>();

    DeptView() {
        // 初始化测试部门数据
        deptList.add(new DeptVo(1, "研发部"));
        deptList.add(new DeptVo(2, "市场部"));
    }

    public static DeptView getInstance() {
        if (singleton == null) {
            singleton = new DeptView();
        }
        return singleton;
    }

    public void list() {
        cache = deptList.toArray(new DeptVo[0]);
        System.out.println("部门 ID\t部门名称");
        for (DeptVo dept : cache) {
            System.out.printf("%d\t%s\n", dept.getId(), dept.getDeptName());
        }
    }

    public DeptVo getCacheBy() {
        System.out.print("请输入部门 ID：");
        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("输入非法，部门 ID 必须为整数！");
            return null;
        }
        int deptId = input.nextInt();
        input.nextLine();

        if (cache != null) {
            for (DeptVo dept : cache) {
                if (dept.getId() == deptId) {
                    return dept;
                }
            }
        }
        System.out.println("未找到对应 ID 的部门！");
        return null;
    }

    public void show() {

    }

    public DeptVo getCacheById() {
        System.out.print("请输入部门 ID：");
        if (!input.hasNextInt()) {
            input.nextLine();
            System.out.println("输入非法，部门 ID 必须为整数！");
            return null;
        }
        int deptId = input.nextInt();
        input.nextLine();

        if (cache != null) {
            for (DeptVo dept : cache) {
                if (dept.getId() == deptId) {
                    return dept;
                }
            }
        }
        System.out.println("未找到对应 ID 的部门！");
        return null;
    }
}